package com.xiguaji.tiktok.v1.web.ins;


import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.v1.ins.blogger.dto.InsBloggerBaseInfo;
import com.xiguaji.tiktok.v1.ins.blogger.dto.InsBloggerRankResponse;
import com.xiguaji.tiktok.v1.ins.blogger.dto.InsBloggerSearchData;
import com.xiguaji.tiktok.v1.ins.blogger.dto.es.InsBlogger;
import com.xiguaji.tiktok.v1.ins.blogger.service.InsBloggerService;
import com.xiguaji.tiktok.v1.ins.blogger.service.TempBloggerService;
import com.xiguaji.tiktok.v1.ins.dic.service.DicService;
import com.xiguaji.tiktok.v1.ins.favorite.dto.FavoriteTypeEnum;
import com.xiguaji.tiktok.v1.ins.favorite.model.InsFavorite;
import com.xiguaji.tiktok.v1.ins.favorite.service.InsFavoriteService;
import com.xiguaji.tiktok.v1.ins.media.dto.InsBloggerMediaInfo;
import com.xiguaji.tiktok.v1.ins.media.service.InsMediaService;
import com.xiguaji.tiktok.v1.product.dto.PlatformTypeEnum;
import com.xiguaji.tiktok.v1.product.dto.ReportTypeEnum;
import com.xiguaji.tiktok.v1.product.service.ProductService;
import com.xiguaji.tiktok.v1.user.dto.UserReturnDto;
import com.xiguaji.tiktok.v1.web.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
public class InsInfluencerController extends InsBaseController {

    @Autowired
    TempBloggerService tempBloggerService;

    @Autowired
    DicService dicService;

    @Autowired
    InsBloggerService insBloggerService;

    @Autowired
    InsMediaService insMediaService;

    @Autowired
    InsFavoriteService insFavoriteService;

    /**
     *
     */
    @Autowired
    ProductService productService;

    @RequestMapping("/influencer/{username}")
    public ModelAndView influencer(@PathVariable String username){
        boolean isUnlock = false;
        ModelAndView modelAndView = getModel(Tab.INS_INFLUENTIAL_DETAIL.getValue());
        // JdbcTemplateHelper.setDataSource(insDataSource);
        modelAndView.setViewName("ins/influencer/index");
//        modelAndView.addObject("blogger",tempBloggerService.getByUsername(username));
        InsBloggerBaseInfo insBloggerBaseInfo = insBloggerService.getUserInfo(username);
        modelAndView.addObject("blogger",insBloggerBaseInfo);
        InsBloggerMediaInfo insBloggerMediaInfo;
        InsBloggerRankResponse insBloggerRankResponse;
        //收藏
        boolean isfavorite = false;
        int favoriteId = 0;
        UserReturnDto userReturnDto = getUserReturn();

        if(null != userReturnDto){
            InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BLOGGER.getCode(),insBloggerBaseInfo.getUid(),userReturnDto.getId().intValue());
            if(null != insFavorite){
                isfavorite = true;
                favoriteId = insFavorite.getId();
            }
        }
        modelAndView.addObject("isFavorite",isfavorite);
        modelAndView.addObject("favoriteId",favoriteId);

        if(insBloggerBaseInfo.getUid() != null && insBloggerBaseInfo.getUid() > 0){
            insBloggerMediaInfo = insMediaService.getInsBloggerMediaInfo(insBloggerBaseInfo.getUid(),2);
            // 是否登入
            if(null != userReturnDto){
                isUnlock = productService.isUnlocked(getUser().getId(),insBloggerBaseInfo.getUid(), PlatformTypeEnum.INS.getCode());
                // 未解锁并是豪华版以上的用户自动解锁
                long roleId = getRoleId();
                if(isUnlock == false){
                    // 角色剩余次数
                    int unlockCount = productService.unlockedNumberByRole(userReturnDto.getId(),roleId,ReportTypeEnum.BLOGGER.getMsg());
                    // 报告次数
                    if(unlockCount < 1){
                        unlockCount = productService.unlockedNumberByReportStarter(userReturnDto.getId());
                    }
                    // 角色解锁
                    if(unlockCount > 0){
                        isUnlock = productService.unlockedByUserRole(userReturnDto.getId(),roleId,insBloggerBaseInfo.getUid(),PlatformTypeEnum.INS.getCode(),ReportTypeEnum.BLOGGER.getCode(),ReportTypeEnum.BLOGGER.getMsg());
                        modelAndView.addObject("user",userReturnDto);
                    }
                    // 报告解锁
                    /*if(!isUnlock){
                        isUnlock = productService.unlockedByReportStarter(userReturnDto.getId(),insBloggerBaseInfo.getUid(), PlatformTypeEnum.INS.getCode(),ReportTypeEnum.BLOGGER.getCode());
                    }*/
                }
            }
            insBloggerRankResponse = insBloggerService.getUserRank(insBloggerBaseInfo.getUid());
        }else{
            insBloggerMediaInfo = new InsBloggerMediaInfo();
            insBloggerRankResponse = new InsBloggerRankResponse();
        }
        insBloggerMediaInfo.setAvgEr(insBloggerBaseInfo.getAvgEr());
        modelAndView.addObject("media",insBloggerMediaInfo);
        modelAndView.addObject("rank",insBloggerRankResponse);
        modelAndView.addObject("numberFormatter",this);

        modelAndView.addObject("isUnlocked",isUnlock);
        return modelAndView;
    }

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public ModelAndView search(){
        ModelAndView modelAndView = getModel(Tab.INS_BLOGGER_SEARCH.getValue());
        Page<InsBlogger> insBloggerPage = insBloggerService.getPageByEs(new InsBloggerSearchData());
        UserReturnDto user = getUserReturn();
        if(null != user){

            insBloggerPage.getList().forEach( insBlogger ->{
                InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BLOGGER.getCode(),insBlogger.getUid(),user.getId().intValue());

                if(null != insFavorite) {
                    insBlogger.setCollectId(insFavorite.getId());
                }
            });
        }
        modelAndView.addObject("listData",insBloggerPage);
        modelAndView.addObject("nations",dicService.getNations(1));
        modelAndView.addObject("languages",dicService.getLanguage(1));
        modelAndView.addObject("category",dicService.getOverallCategory());
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.setViewName("ins/search/index");
        return modelAndView;
    }

    @RequestMapping(value = "/most-followers",method = RequestMethod.GET)
    public ModelAndView mostFollower(){
        ModelAndView modelAndView = getModel(Tab.INS_BLOGGER_SEARCH_MOST_FOLLOWERS.getValue());
        modelAndView.setViewName("ins/follower/most");
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        Page<InsBlogger> insBloggerPage = insBloggerService.getPageByEs(new InsBloggerSearchData());
        UserReturnDto user = getUserReturn();
        if(null != user){

            insBloggerPage.getList().forEach( insBlogger ->{
                InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BLOGGER.getCode(),insBlogger.getUid(),user.getId().intValue());

                if(null != insFavorite) {
                    insBlogger.setCollectId(insFavorite.getId());
                }
            });
        }
        modelAndView.addObject("listData",insBloggerPage);
        return modelAndView;
    }

    @RequestMapping(value = "/most-likes",method = RequestMethod.GET)
    public ModelAndView mostLike(){
        ModelAndView modelAndView = getModel(Tab.INS_BLOGGER_SEARCH_MOST_LIKES.getValue());
        modelAndView.setViewName("ins/like/most");
        InsBloggerSearchData insBloggerSearchData = new InsBloggerSearchData();
        insBloggerSearchData.setSort(1);
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        Page<InsBlogger> insBloggerPage = insBloggerService.getPageByEs(insBloggerSearchData);
        UserReturnDto user = getUserReturn();
        if(null != user){

            insBloggerPage.getList().forEach( insBlogger ->{
                InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BLOGGER.getCode(),insBlogger.getUid(),user.getId().intValue());

                if(null != insFavorite) {
                    insBlogger.setCollectId(insFavorite.getId());
                }
            });
        }
        modelAndView.addObject("listData",insBloggerPage);
        return modelAndView;
    }

    @RequestMapping(value = "/account-search",method = RequestMethod.GET)
    public ModelAndView accountSearch(){
        ModelAndView modelAndView = getModel(Tab.INS_BLOGGER_SEARCH_ACCOUNT.getValue());
        modelAndView.setViewName("ins/search/account");
        InsBloggerSearchData searchData = new InsBloggerSearchData();
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        Page<InsBlogger> insBloggerPage = insBloggerService.getPageByEs(searchData);
        UserReturnDto user = getUserReturn();
        if(null != user){

            insBloggerPage.getList().forEach( insBlogger ->{
                InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BLOGGER.getCode(),insBlogger.getUid(),user.getId().intValue());

                if(null != insFavorite) {
                    insBlogger.setCollectId(insFavorite.getId());
                }
            });
        }
        modelAndView.addObject("listData",insBloggerPage);
        return modelAndView;
    }

    @RequestMapping(value = "/analytics",method = RequestMethod.GET)
    public ModelAndView analytics(){
        ModelAndView modelAndView = getModel(Tab.INS_BLOGGER_SEARCH_ANALYTICS.getValue());
        modelAndView.setViewName("ins/analytics/index");
        return modelAndView;
    }


    @RequestMapping(value = "/favorite",method = RequestMethod.GET)
    public ModelAndView favorite(){
        UserReturnDto user = getUserReturn();

        if(null == user){
            return new ModelAndView("redirect:/");
        }
        ModelAndView modelAndView = getModel(Tab.INS_BLOGGER_FAVORITE.getValue());
        modelAndView.setViewName("favorite/index");
        return modelAndView;
    }

    @RequestMapping(value = "/ad-partner-search",method = RequestMethod.GET)
    public ModelAndView adBloggerSearch(){
        ModelAndView modelAndView = getModel(Tab.INS_AD_BLOGGER_SEARCH.getValue());
        modelAndView.setViewName("ins/search/ad/index");
        InsBloggerSearchData searchData = new InsBloggerSearchData();
        searchData.setSort(5);
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        Page<InsBlogger> insBloggerPage = insBloggerService.getPageByEs(searchData);
        UserReturnDto user = getUserReturn();
        if(null != user){

            insBloggerPage.getList().forEach( insBlogger ->{
                InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BLOGGER.getCode(),insBlogger.getUid(),user.getId().intValue());

                if(null != insFavorite) {
                    insBlogger.setCollectId(insFavorite.getId());
                }
            });
        }
        modelAndView.addObject("listData",insBloggerPage);
        return modelAndView;
    }

    @RequestMapping(value = "/cn",method = RequestMethod.GET)
    public ModelAndView chinaIndex(){
        ModelAndView modelAndView = getModel();
        modelAndView.setViewName("ins/cn/index");
        return modelAndView;
    }

    @RequestMapping(value = "/cn/login",method = RequestMethod.GET)
    public ModelAndView chinaLong(){
        ModelAndView modelAndView = getModel();
        modelAndView.setViewName("ins/cn/login/index");
        return modelAndView;
    }
}
